package com.example.zg.learn.customView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * 描述：
 * zhougan
 * 2019/1/20
 **/
public class ProgressView  extends View{
    private static final String TAG = "ProgressView";
    private int radius=50;//半径
    private Point point;
    private Paint paint;
    private final int defWidth=200;
    private final int defHeight=100;


    public ProgressView(Context context) {
        super(context);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    private void initPaint(){
        if(paint==null){
            paint=new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.RED);
            paint.setStyle(Paint.Style.FILL);
            paint.setStrokeWidth(2);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(paint==null){
            initPaint();
        }
        if(point==null){
            point=new Point(radius,getMeasuredHeight()/2);
        }
        canvas.drawCircle(point.x,point.y,radius,paint);




    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    /**
     * 一般情况重写onMeasure()方法作用是为了自定义View尺寸的规则，
     * 如果你的自定义View的尺寸是根据父控件行为一致，就不需要重写onMeasure()方法
     如果不重写onMeasure方法，那么自定义view的尺寸默认就和父控件一样大小，
     当然也可以在布局文件里面写死宽高，而重写该方法可以根据自己的需求设置自定义view大小
     认识 onMeasure
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        /**
         * 1EXACTLY
         表示父视图希望子视图的大小应该是由specSize的值来决定的，系统默认会按照这个规则来设置子视图的大小，
         简单的说（当设置width或height为match_parent时，模式为EXACTLY，因为子view会占据剩余容器的空间，所以它大小是确定的）
         2. AT_MOST
         表示子视图最多只能是specSize中指定的大小。（当设置为wrap_content时，模式为AT_MOST,
         表示子view的大小最多是多少，这样子view会根据这个上限来设置自己的尺寸）

         3. UNSPECIFIED
         表示开发人员可以将视图按照自己的意愿设置成任意的大小，没有任何限制。这种情况比较少见，不太会用到。
         */
        //最小宽高
        int minWidth = getSuggestedMinimumWidth();
        int minHeight = getSuggestedMinimumHeight();


        int width = measureWidth(minWidth, widthMeasureSpec);
        int height = measureHeight(minHeight, heightMeasureSpec);


        setMeasuredDimension(width,height);
    }

    private int measureWidth(int minWidth,int widthMeasureSpec){
        int width = defWidth;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int specWidth = MeasureSpec.getSize(widthMeasureSpec);//最大值
        switch (widthMode){
            case MeasureSpec.EXACTLY:
                //match_parent
                Log.i(TAG, "measureWidth: EXACTLY");
                width=specWidth;
                break;
            case MeasureSpec.AT_MOST:
                //match_parent
                Log.i(TAG, "measureWidth: AT_MOST");
                width=defWidth;
                break;
            case MeasureSpec.UNSPECIFIED:
                //match_parent
                Log.i(TAG, "measureWidth: UNSPECIFIED");
                width=Math.max(minWidth,defWidth);
                break;
        }



        return width;
    }

    private int measureHeight(int minHeight,int heightMeasureSpec){
        int height=defHeight;
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int specHeight = MeasureSpec.getSize(heightMeasureSpec);//最大值
        switch (heightMode){
            case MeasureSpec.EXACTLY:
                //match_parent 和具体值
                Log.i(TAG, "measureHeight: EXACTLY");
                height=specHeight;
                break;
            case MeasureSpec.AT_MOST:
                //wrap_content
                Log.i(TAG, "measureHeight: AT_MOST");
                height=defHeight;
                break;
            case MeasureSpec.UNSPECIFIED:
                //match_parent
                Log.i(TAG, "measureHeight: UNSPECIFIED");
                height=Math.max(defHeight,minHeight);
                break;
        }

        return  height;
    }
}
